package org.finmanager.core;

import org.finmanager.data.ItemList;

import javax.swing.table.AbstractTableModel;
import java.util.GregorianCalendar;

import static org.finmanager.core.Constants.ITEM_TABLE_COLUMN_NAME;

/**
 * Created by Kelvin on 31/3/2014.
 */

//Defining the FORMAT of the table
public class ItemTableModel extends AbstractTableModel {
	private final ItemList itemList;

	public static final int NAME = 0;
	public static final int DATE = 1;
	public static final int AMOUNT = 2;
	public static final int CATEGORY = 3;

	public ItemTableModel(ItemList itemList) {
		this.itemList = itemList;
	}

	@Override
	public int getRowCount() {
		return itemList.getItemCount();
	}

	@Override
	public int getColumnCount() {
		return ITEM_TABLE_COLUMN_NAME.length;
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		Item item = itemList.getItem(rowIndex);
		switch (columnIndex) {
			case NAME       :return item.getName();
			case DATE       :return item.getDate();
			case AMOUNT     :return item.getAmount();
			case CATEGORY   :return item.getCategory();
			default         :return null;
		}
	}

	@Override
	public String getColumnName(int column) {
		return ITEM_TABLE_COLUMN_NAME[column];
	}

	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return columnIndex == NAME;
	}

	@Override
	public Class<?> getColumnClass(int columnIndex) {
		switch (columnIndex) {
			case NAME       :return String.class;
			case DATE:      return GregorianCalendar.class;
			case AMOUNT     :return Double.class;
			case CATEGORY   :return Category.class;
			default         :return null;
		}
	}
}
